import path from 'path'
import merge from 'webpack-merge'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import ManifestPlugin from 'webpack-manifest-plugin'

import pathConfig from '../config/path';
import baseConfig from './webpack.config.base'
import staticConfig from './webpack.config.static'

export default merge.smart(baseConfig, staticConfig, {
  mode: process.env.NODE_ENV || 'development',
  entry: path.join(pathConfig.clientPath, process.env.RENDER_TYPE ==='SSR' ? 'ssr-client' :'csr-client'),
  output: {
    filename: '[name].[hash].js',
    path: pathConfig.distPath,
    publicPath: '/public/',
  },
  module: {
    rules: [{
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              localIdentName: '[name]_[local]--[hash:base64:5]',
              modules: true
            }
          },
          // 'postcss-loader'
        ]
      },
      // {
      //   test: /\.less$/, // antd 中的样式
      //   use: [
      //     MiniCssExtractPlugin.loader,
      //     'css-loader',
      //     {
      //       loader: 'less-loader',
      //       options: {
      //         paths: [path.resolve(__dirname, '..', 'node_modules', 'antd')],
      //         javascriptEnabled: true,
      //         modifyVars: pathConfig.theme
      //       }
      //     }
      //   ]
      // }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'css/[name].css',
    }),
    new ManifestPlugin({
      fileName: 'asset-manifest.json',
    }),
    new CopyWebpackPlugin([{
        from: `${pathConfig.clientPath}/assets/images`,
        to: `${pathConfig.distPath}/assets/images`,
      },
      {
        context: `${pathConfig.publicPath}`,
        from: '**.*',
        to: `${pathConfig.distPath}`,
      }
    ], {
      ignore: ['index.html']
    }),
    new HtmlWebpackPlugin({
      inject: true,
      template: path.join(pathConfig.publicPath, 'index.html'),
    }),
    new HtmlWebpackPlugin({
      template: '!!ejs-compiled-loader!' + path.join(pathConfig.clientPath, 'ssr-server.ejs'),
      filename: 'ssr-server.ejs'
    }),
  ]
});
